package com.ruoyi.business.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.business.vo.TAuditLogVo;
import com.ruoyi.business.service.ITAuditLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 审批记录Controller
 *
 * @author Tellsea
 * @date 2024-01-26
 */
@Api(tags = "审批记录Controller")
@RestController
@RequestMapping("/business/auditLog")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class TAuditLogController extends BaseController {

    private final ITAuditLogService tAuditLogService;

    @ApiOperation("查询审批记录列表")
    @PreAuthorize("@ss.hasPermi('business:auditLog:list')")
    @GetMapping("/list")
    public TableDataInfo<TAuditLogVo> list(TAuditLogVo entity) {
        return tAuditLogService.queryList(entity);
    }

    @ApiOperation("查询审批记录所有列表")
    @GetMapping("/listAll")
    public AjaxResult listAll(TAuditLogVo entity) {
        return AjaxResult.success("查询成功", tAuditLogService.queryAll(entity));
    }

    @ApiOperation("导出审批记录列表")
    @PreAuthorize("@ss.hasPermi('business:auditLog:export')")
    @Log(title = "审批记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TAuditLogVo entity) {
        List<TAuditLogVo> list = tAuditLogService.queryAll(entity);
        ExcelUtil<TAuditLogVo> util = new ExcelUtil<TAuditLogVo>(TAuditLogVo.class);
        util.exportExcel(response, list, "审批记录数据");
    }

    @ApiOperation("获取审批记录详细信息")
    @PreAuthorize("@ss.hasPermi('business:auditLog:query')")
    @GetMapping(value = "/getInfo/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success("查询成功", tAuditLogService.queryById(id));
    }

    @ApiOperation("获取报名审批记录详细信息")
    @GetMapping(value = "/getAuditList/{id}")
    public AjaxResult getAuditList(@PathVariable("id") Long id) {
        return AjaxResult.success("查询成功", tAuditLogService.listByRegistId(id));
    }

    @ApiOperation("获取所有审批记录详细信息")
//    @PreAuthorize("@ss.hasPermi('business:auditLog:query')")
    @GetMapping(value = "/getAllAuditLogList/{id}")
    public AjaxResult getAllAuditLogList(@PathVariable("id") Long id) {
        Map<String,Object> result = new HashMap<>();
        // 报名审批记录
        result.put("registerLog",tAuditLogService.getRegisterAuditLogList(id));
        // 结业审批记录
        result.put("graduationLog",tAuditLogService.getGraduationAuditLogList(id));
        return AjaxResult.success("查询成功", result);
    }

    @ApiOperation("新增审批记录")
    @PreAuthorize("@ss.hasPermi('business:auditLog:add')")
    @Log(title = "审批记录", businessType = BusinessType.INSERT)
    @PostMapping("add")
    public AjaxResult add(@RequestBody TAuditLogVo entity) {
        return toAjax(tAuditLogService.save(entity));
    }

    @ApiOperation("修改审批记录")
    @PreAuthorize("@ss.hasPermi('business:auditLog:edit')")
    @Log(title = "审批记录", businessType = BusinessType.UPDATE)
    @PostMapping("edit")
    public AjaxResult edit(@RequestBody TAuditLogVo entity) {
        return toAjax(tAuditLogService.updateById(entity));
    }

    @ApiOperation("删除审批记录")
    @PreAuthorize("@ss.hasPermi('business:auditLog:remove')")
    @Log(title = "审批记录", businessType = BusinessType.DELETE)
	@GetMapping("/remove/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tAuditLogService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
